home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9154 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.7 KB  |  170 lines

  1. Path: dns.crocker.com!usenet
  2. From: johns@crocker.com
  3. Newsgroups: comp.lang.c++
  4. Subject: PLEASE help newbie with text file question
  5. Date: 28 Feb 1996 18:26:32 GMT
  6. Organization: Crocker Communciations (crocker.com)
  7. Message-ID: <4h26oo$sot@dns.crocker.com>
  8. Reply-To: johns@crocker.com
  9. NNTP-Posting-Host: iplink015.crocker.com
  10. X-Newsreader: IBM NewsReader/2 v1.2.5
  11.  
  12. I'm currently teaching myself c++, and am practicing using text files with the
  13. following small program. This program contains a simple class which stores 
  14. information about a savings project. My problem is that the value I read in as 
  15. the position of 'balance' in the text file does not seem to be right. It seems 
  16. that I get different results if I use seekp or seekg with the same number. What 
  17. I see is that balance is properly written over 0(the first deposit), but that 
  18. additional deposits are places on the same line as the financial goal. (this is 
  19. followed by two eol chars, one from bal line, another from deposit).
  20. I'm a sophmore in high school and would appreciate ANY suggestions. (the few
  21. people I know that use c++ stay away from streams). This is NOT homework. 
  22. I program for fun. 
  23. I appologize for this lengthy post, but I honestly don't know how else to figure 
  24. out this problem.
  25.     THANK YOU,
  26.     John Szumowski
  27.     johns@crocker.com
  28. ************************************************************************
  29. #include <iostream.h>
  30. #include <fstream.h>
  31. #include <stdlib.h>
  32. #include <values.h>
  33. #include <ctype.h>
  34.  
  35. #define MAXREC 10
  36. #define DATAF "record.txt"
  37.  
  38. class project {
  39.     long bloc;
  40.     char id[16];
  41.     double balance,goal;
  42. public:
  43.     void getpos(long &num) {num = bloc; }
  44.     void getbal(double &b) {b = balance;}
  45.     double percomp(double b,double g) {return ((b/g)*100);}
  46.     void deposit(double d) {balance += d; }
  47.     friend istream &operator>>(istream &stream, project &ob);
  48.     friend ostream &operator<<(ostream &stream, project ob);
  49.     friend fstream &operator>>(fstream &stream, project &ob);
  50.     friend fstream &operator<<(fstream &stream, project &ob);
  51. };
  52.  
  53. istream &operator>>(istream &stream, project &ob)
  54. {
  55.     cout << "Enter a id for this project. (15 char max)\n";
  56.     stream.ignore(MAXINT,'\n');
  57.     stream.getline(ob.id,16);
  58.     cout << "Now, enter the financial goal: $";
  59.     stream >> ob.goal;
  60.     ob.balance = 0.0;
  61.     return stream;
  62. }
  63.  
  64. ostream &operator<<(ostream &stream, project ob)
  65. {
  66.     cout << "ID: " << ob.id << endl;
  67.     cout << "Goal: $" << ob.goal << endl;
  68.     cout << "Balance: $" << ob.balance << endl;
  69.     cout << "Percent Complete: " <<
  70.         ob.percomp(ob.balance, ob.goal) << "%\n";
  71.     return stream;
  72. }
  73.  
  74. fstream &operator>>(fstream &stream, project &ob)
  75. {
  76.     if(!stream.eof()) {
  77.         stream.getline(ob.id, 16);
  78.         stream >> ob.goal;
  79.         ob.bloc = stream.tellg();
  80.         stream >> ob.balance;
  81.         stream.ignore(MAXINT,'\n');
  82.     }
  83.     return stream;
  84. }
  85.  
  86. fstream &operator<<(fstream &stream, project &ob)
  87. {
  88.     stream << ob.id << endl;
  89.     stream << ob.goal << endl;
  90.     ob.bloc = stream.tellp();
  91.     stream << ob.balance << endl;
  92.     return stream;
  93. }
  94.  
  95. void menu(int &i)
  96. {
  97.     cout << "*** PROJECT OPTIONS ***\n";
  98.     cout << "1. Create a new savings project\n";
  99.     cout << "2. Deposit into an existing savings project\n";
  100.     //MORE CHOICES...
  101.     cout << "5. Quit\n";
  102.     cout << "\nYour choice: ";
  103.     cin >> i;
  104. }
  105.  
  106. main()
  107. {
  108.     project fp[MAXREC];
  109.     fstream fpdata;
  110.     int numrec = 0,first_rec = 0,i;
  111.     long location[MAXREC];
  112.  
  113.     //initialize array
  114.     fpdata.open(DATAF,ios::in);
  115.     if(!fpdata) {
  116.         cout << "Unable to open data file!";
  117.         exit(1);
  118.     }
  119.     if(fpdata.peek() == EOF) first_rec = 1;
  120.     while(fpdata >> fp[numrec] && numrec < MAXREC) {
  121.         fp[numrec].getpos(location[numrec]);
  122.         numrec++;
  123.     }
  124.     fpdata.close();
  125.     //main program
  126.     do {
  127.         menu(i);
  128.         //new record
  129.         if(i==1) {
  130.             if(first_rec) fpdata.open(DATAF,ios::out);
  131.             else fpdata.open(DATAF,ios::app);
  132.             cin >> fp[numrec];
  133.             fpdata << fp[numrec];
  134.             fp[numrec].getpos(location[numrec]);
  135.             numrec++;
  136.             fpdata.close();
  137.         }
  138.         //deposit into an existing record
  139.         else if(i==2) {
  140.             char ans;
  141.             int where = 0;
  142.             double dep,bal;
  143.             //show records and ask for the one to be deposited into
  144.             do {
  145.                 cout << "record number: " << (where+1) << endl;
  146.                 cout << fp[where] << endl;
  147.                 cout << "Deposit into this record? y/n --->";
  148.                 cin >> ans;
  149.                 ans = toupper(ans);
  150.                 if(ans!='Y') where++;
  151.             }while(where<numrec && ans!='Y');
  152.             //if a record was chosen, ask for deposit and update file
  153.             if(where!=numrec) {
  154.                 cout << "Amount to deposit: $";
  155.                 cin >> dep;
  156.                 fp[where].deposit(dep);
  157.                 fp[where].getbal(bal);
  158.                 //update bal line in text file
  159.             //opening ios::out destroys previous records
  160.                 fpdata.open(DATAF,ios::in | ios::out);
  161.                 fpdata.seekp(location[where],ios::beg);
  162.                 fpdata << bal << endl;
  163.                 fpdata.close();
  164.             }
  165.         }
  166.     } while(i!=5);
  167.     return 0;
  168. }
  169. ************************************************************************
  170.